package com.huw.cameramanagers;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.handler.IUpdateHandler;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.background.ColorBackground;
import org.anddev.andengine.entity.util.FPSLogger;
import org.anddev.andengine.extension.input.touch.controller.MultiTouch;
import org.anddev.andengine.extension.input.touch.controller.MultiTouchController;
import org.anddev.andengine.extension.input.touch.controller.MultiTouchException;
import org.anddev.andengine.ui.activity.BaseGameActivity;

import android.content.Intent;
import android.hardware.SensorManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.Toast;

import com.badlogic.gdx.math.Vector2;
import com.light.gamecomponents.physics.RollerPhysicsWorld;
import com.light.music.BackGroundMusicController;
import com.light.roller.ResourceManager;


public class BlackStartEndActivity extends BaseGameActivity implements IUpdateHandler{

	public int CAMERA_WIDTH = 720;
	public int CAMERA_HEIGHT = 480;
	public Camera camera;
	protected Scene scene;
	protected RollerPhysicsWorld physicsWorld;
	protected BlackGround black;
	public static enum ActivityState { Init, Lighting , Running, Ending};
	protected ActivityState state = ActivityState.Init;
	protected Class<?> nextActivity;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
	}
	
	@Override
	public Engine onLoadEngine() 
	{
		camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
		final EngineOptions engineOptions = new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), camera);
		engineOptions.setNeedsSound(true);
		engineOptions.setNeedsMusic(true);
		Engine engine = new Engine(engineOptions);
		try {
			if(MultiTouch.isSupported(this)) {
				engine.setTouchController(new MultiTouchController());
				if(MultiTouch.isSupportedDistinct(this)) {
					//Toast.makeText(this, "MultiTouch detected --> Both controls will work properly!", Toast.LENGTH_LONG).show();
				} else {
					//this.mPlaceOnScreenControlsAtDifferentVerticalLocations = true;
				}
			} else {
			}
		} catch (final MultiTouchException e) {
		}
		engineOptions.getTouchOptions().setRunOnUpdateThread(true);
		
		
		black = new BlackGround(camera,this);	
		black.onLoadEngine();
		
		return engine;
	}

	@Override
	public void onLoadResources() {
		ResourceManager.clear(this);
		black.onLoadResources();
	}

	@Override
	public Scene onLoadScene() {
		this.mEngine.registerUpdateHandler(new FPSLogger());
		this.scene = new Scene(2);		
		scene.setBackground(new ColorBackground(0, 0, 0));
		this.physicsWorld = new RollerPhysicsWorld(30, new Vector2(0, SensorManager.GRAVITY_EARTH), false, 3, 2);
		
		
		scene.registerUpdateHandler(this);
		return scene;
	}
	
	
	protected void BlackSceneCover()
	{
		black.onLoadScene(scene, physicsWorld);
		black.onMoveBlack();
	}
	
	public void startGame(Class<?> targetActivity)   
	{ 
		nextActivity = targetActivity;
		state = ActivityState.Ending;		
    }
	
	boolean isForMenu = false;
	public void startMenu(Class<?> targetActivity) 
	{ 
		isForMenu = true;
		nextActivity = targetActivity;
		state = ActivityState.Ending;		
    }

	
	private void startGame2() 
	{ 
		if (!isForMenu)
		{
			BackGroundMusicController.changeMusic(MediaPlayer.create(this, com.light.roller.R.raw.zikweb));

		}
		else
			isForMenu = false;
		
		
		ResourceManager.unLoadAllSounds();
		this.finish(); 
		this.startActivity(new Intent(this, nextActivity)); 		
		
    }

	@Override
    protected void onStop() {
    	ResourceManager.unLoadAllSounds();
    	ResourceManager.onStop();
        super.onStop();
    }
	
	@Override
	public void onLoadComplete() {
		// TODO Auto-generated method stub
		
	}
	

	@Override
	public void onUpdate(float pSecondsElapsed) {
		switch(state)
		{
		case Init:
			state = ActivityState.Lighting;			
			break;
		case Lighting:
			if(black.isLightOff())black.LightOnCamera();
			state = ActivityState.Running;			
		case Running:
			break;
		case Ending:
			if(black.isLightOn())black.LightOffCamera();
			else if(black.isLightOff())
			{
				startGame2();
				state = ActivityState.Running;
			}				
			break;
		}
		black.onMoveBlack();
		
	}

	@Override
	public void reset() {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
	     if (keyCode == KeyEvent.KEYCODE_BACK ||
	    	 keyCode == KeyEvent.KEYCODE_HOME)
	    	 BackGroundMusicController.Stop();

	     return super.onKeyDown(keyCode, event);    
	}
	@Override
	protected void onPause()
	{
		if (BackGroundMusicController.turnOffOnPause)
			BackGroundMusicController.Stop();
		else
			BackGroundMusicController.turnOffOnPause = true;
		super.onPause();
	}
	
}
